<?php

/**
 * index
 * @author 刘军 
 * @version 1.0
 * @package application
 */
class Member extends Membr_Controller
{
	public function __construct() {
		parent::__construct();
		$this->load->model('member_model');
	}
	
	//会员中心
	public function index($flag=0){
		
		$search = array();
		$search['orders'] = array('created' => 'desc');
		$search['attributes']['member_id '] = $this->session->userdata('user_id');
		$flag = intval($flag);
		//0:全部,1:未审核,2:已审核,3:被退回
		if($flag<0 || $flag>3){
			header('location:' . base_url($this->_site_path . '/member/index'));
		}elseif($flag==0){
			$search['attributes']['status'] = '';
		}else{
			$search['attributes']['status'] = $flag;
		}
		$data = array();
		$data['flag'] = $flag;
		$this->load->model('member_article_model');
		// 总数与分页
		$total = $this->member_article_model->total( null, $search );
		$data['total'] = $total;
		// 请使用config_item( 'per_page' )获取全局显示条数
		//$per_page = config_item( 'per_page' ) ;
		$per_page = 10;
		$this->load->library( 'pagination' );
		$pagination_config = array(
				'base_url' => base_url($this->_site_path . '/member/index/' . $flag),
				'total_rows' => $total,
				'per_page' => $per_page,
				'uri_segment' => 5,
		);
		//分页
		$this->pagination->initialize( $pagination_config );
		$data['pagination'] = $this->pagination->create_links();
		$search['limit'] = array( 'persize' => $per_page, 'offset' => $this->pagination->get_cur_offset() );
		$data['lists'] = $this->member_article_model->all($search );
		
		foreach ($data['lists'] as $k=>$v){
			switch ($v['status']){
				case 1:
					$data['lists'][$k]['status_show'] = "<font color='#06f'>未审核</font>";
					break;
				case 2:
					$data['lists'][$k]['status_show'] = "<font color='#80B424'>已审核</font>";
					break;
				case 3:
					$data['lists'][$k]['status_show'] = "<font color='#f00'>被退回</font>";
					break;
			}
		}
		$this->load->view($this->_site_path . '/member/index', $data);
	}
	
	//文章操作
	public function article_operate($flag, $id=0){
		
		if($flag!='add' && intval($id)<=0){
			header('location:' . base_url($this->_site_path . '/member/index'));
		}
		$data = array();
		$this->load->model('member_article_model');
		switch ($flag){
			//增加
			case 'add':
				$article = $this->input->post();
				if($article){
					$article['member_id'] = $this->session->userdata('user_id');
					$article['created'] = $article['modified'] = time();
					if(!$this->member_article_model->insert($article)){
						$this->error_js('文章发表失败！');exit;
					}
					header('location:' . base_url($this->_site_path . '/member/index'));
				}
				$data['flag'] = $flag;
				$this->load->view($this->_site_path . '/member/article_info', $data);
				break;
			//修改
			case 'edit':
				$article = $this->input->post();
				if($article){
					$article['modified'] = time();
					if(!$this->member_article_model->update_by_pk($article, $id)){
						$this->error_js('文章修改失败！');exit;
					}
					header('location:' . base_url($this->_site_path . '/member/index'));
				}
				$data['article'] = $this->member_article_model->get_by_pk($id);
				$data['flag'] = $flag;
				$this->load->view($this->_site_path . '/member/article_info', $data);
				break;
			//删除
			case 'del':
				if(!$this->member_article_model->delete_by_pk($id)){
					$this->error_js('文章删除失败！');exit;
				}
				header('location:' . base_url($this->_site_path . '/member/index'));
				break;
			default:
				header('location:' . base_url($this->_site_path . '/member/index'));
				break;
		}
	}
}